﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tycho.Peyote {
    /// <summary>
    /// Interaction logic for ArrowAnnotationControl.xaml
    /// </summary>
    public partial class ArrowAnnotationControl : UserControl {
        public bool StartHorizontal { get; set; }
        public bool EndHorizontal { get; set; }
        Point Start;

        public ArrowAnnotationControl () {
            InitializeComponent ();

            Start = new Point (0, 0);

            DataContext = this;
        }

        Point End {
            get {
                return new Point (Canvas.GetRight (this) - Canvas.GetLeft (this), Canvas.GetBottom (this) - Canvas.GetTop (this));
            }
        }

        Point BezierPoint1 {
            get {
                if (StartHorizontal) {
                    return new Point (Start.X + (End.X - Start.X) / 2, Start.Y);
                } else {
                    return new Point (Start.X, Start.Y + (End.Y - Start.Y) / 2);
                }
            }
        }

        Point BezierPoint2 {
            get {
                if (EndHorizontal) {
                    return new Point (End.X - (End.X - Start.X) / 2, End.Y);
                } else {
                    return new Point (End.X, End.Y - (End.Y - Start.Y) / 2);
                }
            }
        }

        public DrawingImage LinkDrawing {
            get {
                PathFigure curveFigure = new PathFigure ();
                curveFigure.Segments.Add (new BezierSegment (BezierPoint1, BezierPoint2, End, true));
                PathGeometry curveGeometry = new PathGeometry ();
                curveGeometry.Figures.Add (curveFigure);
                Pen curvePen = new Pen (Brushes.Red, 2);
                curvePen.DashStyle = new DashStyle (new double [] { 2, 3 }, 0);
                curvePen.DashCap = PenLineCap.Round;
                GeometryDrawing curveDrawing = new GeometryDrawing (null, curvePen, curveGeometry);

                EllipseGeometry startDot = new EllipseGeometry (Start, 2, 2);
                EllipseGeometry endDot = new EllipseGeometry (End, 2, 2);
                GeometryGroup dots = new GeometryGroup ();
                dots.Children.Add (startDot);
                dots.Children.Add (endDot);
                GeometryDrawing dotsDrawing = new GeometryDrawing (Brushes.Red, null, dots);

                DrawingGroup curveDots = new DrawingGroup ();
                curveDots.Children.Add (curveDrawing);
                curveDots.Children.Add (dotsDrawing);

                return new DrawingImage (curveDots);
            }
        }
    }
}
